package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;

/**
 * Created by admin on 2016/12/23.
 */
public class SubjectListAdapter extends BaseAdapter {
    private Context context;
    private View.OnClickListener listener;
    private SubjectAdapter mAdapter;
    private BaseActivity activity;

    public SubjectListAdapter(Context context, View.OnClickListener listener) {
        this.context = context;
        this.listener = listener;
        activity = (BaseActivity) context;
    }


    @Override
    public int getCount() {
        return 1;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SubjectListViewHolder viewHolder = new SubjectListViewHolder();
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.subject_item, null);
            viewHolder.tv_all_number = (TextView) convertView.findViewById(R.id.tv_all_number);
            viewHolder.tv_add = (TextView) convertView.findViewById(R.id.tv_add);
            viewHolder.subject_list = (SwipeMenuListView) convertView.findViewById(R.id.subject_list);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (SubjectListViewHolder) convertView.getTag();
        }
        mAdapter = new SubjectAdapter(context);
        viewHolder.subject_list.setAdapter(mAdapter);

        SwipeMenuCreator creater = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                // create置顶item
                SwipeMenuItem item1 = new SwipeMenuItem(context);
                // set item background
                item1.setBackground(new ColorDrawable(0xff008c99));
                // set item width
                item1.setWidth(dp2px(90));
                // set item title
                item1.setTitle("替换");
                // set item title fontsize
                item1.setTitleSize(18);
                // set item title font color
                item1.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(item1);

                //同理create删除item
                SwipeMenuItem deleteItem = new SwipeMenuItem(context);
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(dp2px(90));
                // set a icon
                deleteItem.setIcon(R.mipmap.ic_delete);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };
        viewHolder.subject_list.setMenuCreator(creater);

        viewHolder.subject_list.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        activity.MyToast("替换" + position);
//                        Intent intent = new Intent(context, ReplaceActivity.class);
//                        context.startActivity(intent);
                        mAdapter.notifyDataSetChanged();
                        break;

                    case 1:
                        //删除的逻辑
                        activity.MyToast("删除" + position);
                        mAdapter.notifyDataSetChanged();
                        break;

                    default:
                        break;
                }
                return false;
            }
        });
        viewHolder.tv_add.setText("");
        viewHolder.tv_add.setOnClickListener(listener);
        viewHolder.tv_add.setTag(position);
        return convertView;
    }

    class SubjectListViewHolder {
        TextView tv_all_number;
        TextView tv_add;
        SwipeMenuListView subject_list;
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, activity.getResources().getDisplayMetrics());
    }

}
